package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BaseAlternateAirport;
import com.ruoyi.base.service.IBaseAlternateAirportService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 备降机场信息;Controller
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@Api(description="备降机场信息管理")
@RestController
@RequestMapping("/base/alternateAirport")
public class BaseAlternateAirportController extends BaseController
{
    @Autowired
    private IBaseAlternateAirportService baseAlternateAirportService;

    /**
     * 查询备降机场信息;列表
     */
    @ApiOperation("查询备降机场信息")
    @GetMapping("/list")
    public TableDataInfo list(BaseAlternateAirport baseAlternateAirport)
    {
        startPage();
        baseAlternateAirport.setUnitCode(getLoginUser().getUser().getUnit());
        List<BaseAlternateAirport> list = baseAlternateAirportService.selectBaseAlternateAirportList(baseAlternateAirport);
        return getDataTable(list);
    }

    /**
     * 导出备降机场信息;列表
     */
    @ApiOperation("导出备降机场信息")
    @Log(title = "备降机场信息;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseAlternateAirport baseAlternateAirport)
    {
        List<BaseAlternateAirport> list = baseAlternateAirportService.selectBaseAlternateAirportList(baseAlternateAirport);
        ExcelUtil<BaseAlternateAirport> util = new ExcelUtil<BaseAlternateAirport>(BaseAlternateAirport.class);
        util.exportExcel(response, list, "备降机场信息;数据");
    }

    /**
     * 获取备降机场信息;详细信息
     */
    @ApiOperation("获取备降机场信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(baseAlternateAirportService.selectBaseAlternateAirportByID(ID));
    }

    /**
     * 新增备降机场信息;
     */
    @ApiOperation("新增备降机场信息")
    @Log(title = "备降机场信息;", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseAlternateAirport baseAlternateAirport)
    {
        baseAlternateAirport.setID(UUID.randomUUID().toString().replace("-", ""));
        baseAlternateAirport.setCreatedBy(getUserId().toString());
        baseAlternateAirport.setCreatedTime(new Date());
        baseAlternateAirport.setUnitCode(getLoginUser().getUser().getUnit());
        return toAjax(baseAlternateAirportService.insertBaseAlternateAirport(baseAlternateAirport));
    }

    /**
     * 修改备降机场信息;
     */
    @ApiOperation("修改备降机场信息")
    @Log(title = "备降机场信息;", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseAlternateAirport baseAlternateAirport)
    {
        baseAlternateAirport.setUpdatedBy(getUserId().toString());
        baseAlternateAirport.setUpdatedTime(new Date());
        return toAjax(baseAlternateAirportService.updateBaseAlternateAirport(baseAlternateAirport));
    }

    /**
     * 删除备降机场信息;
     */
    @ApiOperation("删除备降机场信息")
    @Log(title = "备降机场信息;", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(baseAlternateAirportService.deleteBaseAlternateAirportByIDs(IDs));
    }


    @Log(title = "主机场管理", businessType = BusinessType.IMPORT)
    @ApiOperation("备降机场导入")
    @PostMapping(value = "/importData", consumes = "multipart/form-data")
    public AjaxResult importData(@ApiParam(name="file", value = "文件", required = true) MultipartFile file,
                                 @ApiParam(name="updateSupport", value = "是否支持更新数据（true:支持，false:不支持）", required = true)boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseAlternateAirport> util = new ExcelUtil<BaseAlternateAirport>(BaseAlternateAirport.class);
        List<BaseAlternateAirport> airportList = util.importExcel(file.getInputStream());
        String operId = getUserId().toString();
        String message = baseAlternateAirportService.importMainAirport(airportList, updateSupport, operId);
        return success(message);
    }
}
